package com.console.operations.planmgr;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.console.pages.planmgr.FinancePlanLoanAuditPage;

	/**
		@author 王嘉犇
		@name 钜宝盆后台理财计划债权审核
		@type 动作类
	 */

public class FinancePlanLoanAudit extends OperationBase {
	
	public FinancePlanLoanAudit(UITestBase tb){
		super(tb);
	}
	
	//判断债券状态是否变为10(招标中)
	public boolean isloanstatuscorrectafteraudit = false;

	public void audit() throws Exception{
		switchToDefaultContent();
		FinancePlanLoanAuditPage fpbap = new FinancePlanLoanAuditPage(tb);
		waitForElementDisplayed(fpbap.titledv);
		fpbap.titledv.click();
		//得到最早一条未审核债权的序列号、债权名称以及借款人手机号
		Map<String, String> rowmap = DBUtil.queryOneRow("select l.vc_sequence sequence, l.vc_name loanname, l.nb_amount nbamount, l.nb_rate nbrate, u.vc_cellphone cellphone, u.vc_name username\n"
				   									  + "from fiz_loan l, fiz_user u\n"
				   									  + "where l.dc_status = ?\n"
				   									  + "  and l.dc_type = ?\n"
				   									  + "  and l.pk_id in (\n"
				   									  + "				   select fk_loan_id\n"
				   									  + "				   from fiz_loan_prop\n"
				   									  + "				   where vc_code = 'direct'\n"
				   									  + "				     and vc_value = '01'\n"
				   									  + "			      )\n"
				   									  + "  and l.fk_user_id = u.pk_id\n"
				   									  + "order by l.vc_sequence asc limit 1", new Object[]{"00", "03"});
		//得到最早一条债权的债权号
		String sequence = rowmap.get("sequence");
		log.info("理财计划债权序列号为  " + sequence);
		//得到最早一条债权的名称
		String financeplanloanname = rowmap.get("loanname");
		log.info("理财计划债权名称为  " + financeplanloanname);
		//得到最早一条债权的限额
		String nbamount = rowmap.get("nbamount");
		//债权限额变成逗号分隔的格式
		String havecommaformatnbamount = havacommanumformat(nbamount);
		log.info("理财计划债权限额的逗号显示格式为  " + havecommaformatnbamount);
		//得到最早一条债权的年化利率
		String nbrate = rowmap.get("nbrate");
		String percentformatnbrate = pencentformat(nbrate);
		log.info("理财计划债权年化利率百分号显示格式为 " + percentformatnbrate);
		//得到最早一条债权的借款人手机号
		String cellphone = rowmap.get("cellphone");
		log.info("借款人手机号为  " + cellphone);
		//得到最早一条债权的借款人姓名
		String username = rowmap.get("username");
		log.info("借款人姓名为  " + username);
		fpbap.fuzzyplanloannameinput.type(financeplanloanname);
		fpbap.fuzzycellphoneinput.type(cellphone);
		fpbap.searchBtn.click();
		pause(800);
		//点击搜索按钮之后，底下显示总计的tr可以点击以防刷新失效
		waitForElementelementToBeClickable(fpbap.belowsumtd);
		waitForElementelementToBeClickable(fpbap.lastpageLink);
		//得到债权审核页面的分页页数
		int pagecount = Integer.parseInt(fpbap.lastpageLink.getText());
		log.info("债权审核页的页数为  " + pagecount);
		//设置一个是否找到债权的标志位
		boolean isfound = false;
		//定位序列号控件的radio的findName
		fpbap.aimPlanLoanradio.setfindName("xpath");
		//定位序列号控件的radio的findValue
		fpbap.aimPlanLoanradio.setfindValue("//tr[td[text()='" + sequence + "'] and td[text()='" + financeplanloanname + "'] and td[text()='" + username + ""
										  + "'] and td[text()='" + havecommaformatnbamount + "'] and td[text()='" + percentformatnbrate + "']]/td[1]/input");
		//非分页情况
		if(pagecount==1){
			log.info("不存在分页的情况");
			if(isPresent(fpbap.aimPlanLoanradio)){
				isfound = true;
			}
		}else{
			log.info("存在分页的情况");
			//分页查找是有有对应债权序列号的
			for(int i=1;i<=pagecount;i++){
				fpbap.presentpageLinkli.setfindName("xpath");
				fpbap.presentpageLinkli.setfindValue("//ul[@class='pagination']/li[a[text()='" + i + "']]");
				fpbap.presentpageLink.setfindName("linkText");
				fpbap.presentpageLink.setfindValue(String.valueOf(i));
				waitForElementDisplayed(fpbap.presentpageLinkli);
				if(i!=1){
					fpbap.presentpageLink.click();
					log.info("点击第" + i + "页链接");
				}
				waitForElementelementToBeClickable(fpbap.presentpageLinkli);
				waitForPageLinkclicked(fpbap.presentpageLinkli);
				if(isPresent(fpbap.aimPlanLoanradio)){
					isfound = true;
					break;
				}
			}
		}
		if(!isfound){
			handlefailure("理财计划债权审核页面上找不到该债权序列号");
		}
		fpbap.aimPlanLoanradio.click();
		fpbap.passselectedBtn.click();
		waitForElementDisplayed(fpbap.submitBtn);
		fpbap.submitBtn.click();
		waitForPageContainsStr("通过审核");
		String loanstatus = DBUtil.queryOneCell("select dc_status\n"
											  + "from fiz_loan\n"
											  + "where vc_sequence = ?", new Object[]{sequence});
		isloanstatuscorrectafteraudit = loanstatus.equals("10");
	}
	
	/**
	 * 逗号分隔显示数字格式
	 */
	private String havacommanumformat(String numstr) {
		BigDecimal numstrbd = new BigDecimal(numstr).setScale(2, BigDecimal.ROUND_DOWN);
        DecimalFormat df = new DecimalFormat("#,###.00"); 
        return df.format(numstrbd.doubleValue());
    }
	
	/**
	 * 数字加百分号格式
	 */
	private String pencentformat(String numstr){
		BigDecimal numstrbd = new BigDecimal(numstr).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_DOWN);
		return numstrbd.toString() + "%";
	}
}
